using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WOWEquipOptimizer
{
    /// <summary>
    /// Form to create black and white lists for m_Items, fairly self explaining
    /// </summary>
    public partial class BlackWhitelistDialog : Form
    {
        private List<Item> m_Items;

        public List<Item> Items
        {
            set
            {
                m_Items = value;
            }
        }
        private List<string> m_ItemWhitelist = new List<string>();

        public List<string> ItemWhitelist
        {
            get { return m_ItemWhitelist; }
            set
            {
                m_ItemWhitelist.Clear();
                m_ItemWhitelist.AddRange(value);
            }
        }
        private List<string> m_ItemBlacklist = new List<string>();

        public List<string> ItemBlacklist
        {
            get { return m_ItemBlacklist; }
            set
            {
                m_ItemBlacklist.Clear();
                m_ItemBlacklist.AddRange(value);
            }
        }
        public BlackWhitelistDialog()
        {
            InitializeComponent();
            ItemListBox.Items.Clear();
        }
        public void Populate()
        {
            if (ItemListBox.Items.Count == 0)
            {
                ItemListBox.ValueMember = "Name";
                ItemListBox.Items.AddRange(Database.Items.ToArray());
            }
        }
        private void DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            Item item = (Item)ItemListBox.Items[e.Index];
            if (item == null) return;
            Brush myBrush = Brushes.Black;
            if (m_Items.Contains(item) && !m_ItemWhitelist.Contains(item.Name) && !m_ItemBlacklist.Contains(item.Name))
            {
                myBrush = Brushes.SpringGreen;
            }
            else if (m_ItemWhitelist.Contains(item.Name))
            {
                myBrush = Brushes.Green;
            }
            else if (m_ItemBlacklist.Contains(item.Name))
            {
                myBrush = Brushes.Red;
            }
            else myBrush = Brushes.Coral;
            e.Graphics.DrawString(item.Name, e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
            e.DrawFocusRectangle();
        }

        private void ItemDoubleClicked(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                Item item = (Item)ItemListBox.SelectedItem;
                if (m_Items.Contains(item) && !m_ItemWhitelist.Contains(item.Name) && !m_ItemBlacklist.Contains(item.Name)) // spring green
                {
                    m_ItemBlacklist.Add(item.Name); // blacklist it now
                }
                else if (m_ItemBlacklist.Contains(item.Name)) // blacklisted
                {
                    m_ItemBlacklist.Remove(item.Name); // remove blacklist
                    if (!m_Items.Contains(item)) m_Items.Add(item);
                }
                else if (m_ItemWhitelist.Contains(item.Name)) // whitelisted
                {
                    m_ItemWhitelist.Remove(item.Name); // remove whitelist
                    if (m_Items.Contains(item)) m_Items.Remove(item);
                }
                else
                {
                    m_ItemWhitelist.Add(item.Name);
                }
                Invalidate();
            }
        }
    }
}